﻿package crochet.core;

/*
 Copyright 2011 Intuitive Solutions

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.util.Timer;

import crochet.core.policy.ExecutionPolicyFactory;
import crochet.core.policy.IExecutionPolicy;

public class TimerScheduler implements IScheduler {
	protected Timer tTimer;
	protected boolean bRepeat;
	// protected TimeSpan tsDueTime;
	protected int iRepeatCount, iCurrentIteration;
	protected IExecutionPolicy epExecutionPolicy, epCloneableExecutionPolicy;

	public TimerScheduler(int iDueTime, boolean bRepeat) {
		// this(iDueTime, bRepeat, Timeout.Infinite,
		// ExecutionPolicyFactory.getInstance().Create(null, null));
	}

	public TimerScheduler(int iDueTime, boolean bRepeat,
			IExecutionPolicy epCloneableExecutionPolicy) {
		// this(iDueTime, bRepeat, Timeout.Infinite,
		// epCloneableExecutionPolicy);
	}

	public TimerScheduler(int iDueTime, boolean bRepeat, int iRepeatCount) {
		this(iDueTime, bRepeat, iRepeatCount, ExecutionPolicyFactory
				.getInstance().Create(null, null));
	}

	public TimerScheduler(int iDueTime, boolean bRepeat, int iRepeatCount,
			IExecutionPolicy epCloneableExecutionPolicy) {
		/*
		 * this.bRepeat = bRepeat; this.iRepeatCount = iRepeatCount;
		 * this.epCloneableExecutionPolicy = epCloneableExecutionPolicy;
		 * this.tsDueTime = TimeSpan.FromMilliseconds(iDueTime); tTimer = new
		 * Timer(new TimerCallback(SchedulerThread));
		 * 
		 * if (epCloneableExecutionPolicy.getRootScheduler() == null) {
		 * epCloneableExecutionPolicy.setRootScheduler(this); }
		 */
	}

	public TimerScheduler(/* TimeSpan tsDueTime, */boolean bRepeat,
			IExecutionPolicy epCloneableExecutionPolicy)

	{
		// this(/*tsDueTime, */bRepeat, Timeout.Infinite,
		// epCloneableExecutionPolicy);
	}

	public TimerScheduler(/* TimeSpan tsDueTime, */boolean bRepeat,
			int iRepeatCount, IExecutionPolicy epCloneableExecutionPolicy) {
		/*
		 * this.bRepeat = bRepeat; this.tsDueTime = tsDueTime; this.iRepeatCount
		 * = iRepeatCount; this.epCloneableExecutionPolicy =
		 * epCloneableExecutionPolicy; tTimer = new Timer(new
		 * TimerCallback(SchedulerThread));
		 * 
		 * if (epCloneableExecutionPolicy.getRootScheduler() == null) {
		 * epCloneableExecutionPolicy.setRootScheduler(this); }
		 */
	}

	/*
	 * public TimeSpan getDueTime() { return tsDueTime; }
	 * 
	 * public void setDueTime(TimeSpan tsDueTime) { this.tsDueTime = tsDueTime;
	 * }
	 */

	public IExecutionPolicy getCloneableExecutionPolicy() {
		return epCloneableExecutionPolicy;
	}

	public void schedule(IExecutionPolicy epExecutionPolicy) {
		this.epExecutionPolicy = epExecutionPolicy;

		Start();
	}

	public void schedule(ISchedulerDelegate sdSchedulerDelegate) {
		schedule(sdSchedulerDelegate, null);
	}

	public void schedule(ISchedulerDelegate sdSchedulerDelegate,
			IExceptionHandlerDelegate ehdExceptionHandlerDelegate) {
		schedule(epCloneableExecutionPolicy.clone(sdSchedulerDelegate,
				ehdExceptionHandlerDelegate));
	}

	public void Start() {
		/*
		 * if (!bRepeat) { tTimer.Change(tsDueTime,
		 * TimeSpan.FromMilliseconds(-1)); } else { tTimer.Change(tsDueTime,
		 * tsDueTime); }
		 */
	}

	public void Stop() {
		/*
		 * epExecutionPolicy = null;
		 * 
		 * tTimer.Change(Timeout.Infinite, Timeout.Infinite);
		 */
	}

	public void SchedulerThread(Object oObject) {
		/*
		 * if (epExecutionPolicy != null) { epExecutionPolicy.execute(this); }
		 * 
		 * if (iRepeatCount != Timeout.Infinite && iCurrentIteration++ ==
		 * iRepeatCount - 1) { tTimer.Change(Timeout.Infinite,
		 * Timeout.Infinite); tTimer.dispose(); }
		 */
	}

	public void dispose() {
		/*
		 * epExecutionPolicy = null; iRepeatCount = Timeout.Infinite;
		 * 
		 * tTimer.Change(Timeout.Infinite, Timeout.Infinite); tTimer.dispose();
		 * 
		 * tTimer = null;
		 */
	}
}
